package cn.yh.shop.admin.action;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.yh.shop.admin.model.News;
import cn.yh.shop.admin.service.impl.NewsServiceImpl;

/** 
*@Description:新闻管理控制器
* @author  "许荣斐"
* @time:2016年12月15日 下午4:03:07
* @version 1.0 
*/
public class NewsAction extends ActionSupport implements ModelDriven<News>{
	
	 private static Logger logger = LogManager.getLogger(NewsAction.class);
	 
	/**
	 * @Description: news : 实体模型
	 */
	private News news;
	
	private JSONObject jsob;
	

	/**
	 * get方法
	 * @return  jsob
	 */
	public JSONObject getJsob() {
		return jsob;
	}

	/**
	 * @Description: newsServiceImpl : 引入NewsServiceImpl
	 */
	@Autowired
	private NewsServiceImpl newsServiceImpl;
	
	/**
	 * @Description: dataMap : map变量,用于作为json输出
	 */
	private Map<String, Object> dataMap;
	
	/**
	 * @Description:无参构造方法;实例化map
	 */
	public NewsAction() {
		dataMap=new HashMap<String, Object>();
	}
	
	/**
	 * @Description:添加新闻头条
	 * @author: "许荣斐"
	 * @time:2016年12月16日 下午6:56:36
	 */
	public String addNews(){
		String retun="";
		jsob=new JSONObject();
			if (news!=null) {
				int addNews = newsServiceImpl.addNews(news);
				
				if (addNews>0) {
					jsob.put("msg", "添加成功!");
					logger.info("news 添加成功!!");
					retun= "success";
				}else{
					jsob.put("msg", "添加失败!");
					logger.info("news 添加失败!!");
					retun= "error";
				}
			}else{
				jsob.put("msg", "添加失败!");
				logger.info("news 为空!");
				retun= "error";
			}
			return retun;
	}

	

	/**
	 * @param news the news to set
	 */
	public void setNews(News news) {
		this.news = news;
	}

	/**
	 * @return the dataMap
	 */
	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	@Override
	public News getModel() {
		if (news==null) {
			news=new News();
		}
		return news;
	}

	
	
}
